<template>
    <div>
        <el-tree :data="data" :default-checked-keys="ruleForm.menuId" show-checkbox default-expand-all node-key="menuId"
            ref="tree" highlight-current :props="defaultProps">
        </el-tree>

    </div>
</template>

<script>
    import { Query } from '../../api/menu';
    import { QueryByRole, AssignPermission } from '../../api/role';
    export default {
        name: 'RbacUiAssignPermission',
        props: {
            roleId: {
                type: Number
            }
        },
        data() {
            return {
                data: [],
                ruleForm: {
                    roleId: 0,
                    menuId: []
                },
                defaultProps: {
                    children: 'children',
                    label: 'menuName'
                }
            };
        },

        mounted() {
            this.ruleForm.roleId = this.roleId;
            //树
            Query().then(x => {
                this.data = x.data;
            });
            QueryByRole({ roleId: this.roleId }).then(m => {
                this.ruleForm.menuId = m.data;
            });
        },

        methods: {
            submitForm() {
                this.ruleForm.menuId = this.$refs.tree.getCheckedKeys(true);
                AssignPermission(this.ruleForm).then(m => {
                    let res = m.data;
                    if (res.code == 0) {
                        this.$message({
                            message: res.msg,
                            type: 'warning'
                        });
                    } else {
                        let _this = this;
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            onClose: function (msg) {
                                _this.$emit('OnSave', res, 'permission');
                            }
                        });
                    }
                });
            }
        },
    };
</script>